/*
 * File:   SettingDlg.cpp
 * Author: Morpheus
 *
 * Created on 25 juin 2013, 08:31
 */

#include "SettingDlg.h"
#include "QFileInfeSorterFactory.h"
#include <QtPrintSupport/qprinter.h>
#include <QtGui/qimagewriter.h>
#include <QtGui/qtextdocumentwriter.h>

SettingDlg::SettingDlg(QWidget *parent)
    : QDialog(parent) {
    widget.setupUi(this);

    QPrinter printer;
    foreach(int r, printer.supportedResolutions()){
        widget.resolution->addItem(tr("%1 p").arg(r), r);
    }
    QStringList listSupport;
    listSupport << tr("PDF") << tr("SVG") << tr("PS");
    foreach(QByteArray supported, QTextDocumentWriter::supportedDocumentFormats()){
        listSupport << QString::fromLatin1(supported).toUpper();
    }
    foreach(QByteArray supported, QImageWriter::supportedImageFormats()){
        listSupport << QString::fromLatin1(supported).toUpper();
    }
    widget.defaultSaveFormat->addItems(listSupport);

    widget.sorterFileId->addItems(QFileInfeSorterFactory::getSorterDescriptions());
}

SettingDlg::~SettingDlg() {
}

MyBatchSetting SettingDlg::getBatchSetting() const{
    return m_setting;
}

void SettingDlg::setBatchSetting(const MyBatchSetting& v){
    m_setting = v;

    widget.browseFolderNative->setChecked(m_setting.getBrowseFolderNative());
    widget.color->setCurrentIndex(m_setting.getColor());
    widget.createScriptBefore->setChecked(m_setting.getCreateScriptBefore());
    widget.defaultSaveFormat->setCurrentText(m_setting.getDefaultSaveFormat());
    widget.defaultSavePatern->setText(m_setting.getDefaultSavePatern());
    widget.margingBottom->setValue(m_setting.getMargingBottom());
    widget.margingLeft->setValue(m_setting.getMargingLeft());
    widget.margingRight->setValue(m_setting.getMargingRight());
    widget.margingTop->setValue(m_setting.getMargingTop());
    widget.mdiShowMode->setCurrentIndex(m_setting.getMdiShowMode());
    widget.orientation->setCurrentIndex(m_setting.getOrientation());
    widget.paperSize->setCurrentIndex(m_setting.getPaperSize());
    int idx = widget.resolution->findData(m_setting.getResolution());
    widget.resolution->setCurrentIndex(idx);
    widget.scriptBeforePatern->setText(m_setting.getScriptBeforePatern());
    widget.sorterFileId->setCurrentIndex(m_setting.getSorterFileId());
    widget.sorterFileParam->setText(m_setting.getSorterFileParam());
}

void SettingDlg::on_browseFolderNative_stateChanged(const int& v){
    if(m_setting.getBrowseFolderNative() != (v==Qt::Checked)){
        m_setting.setBrowseFolderNative(v==Qt::Checked);
    }
}
void SettingDlg::on_color_currentIndexChanged(const int& v){
    if(m_setting.getColor() != v){
        m_setting.setColor(v);
    }
}
void SettingDlg::on_sorterFileId_currentIndexChanged(const int& v){
    if(m_setting.getSorterFileId() != v){
        m_setting.setSorterFileId(v);
    }
}
void SettingDlg::on_sorterFileParam_textChanged(const QString& v){
    if(m_setting.getSorterFileParam() != v){
        m_setting.setSorterFileParam(v);
    }
}

void SettingDlg::on_createScriptBefore_stateChanged(const int& v){
    if(m_setting.getCreateScriptBefore() != (v==Qt::Checked)){
        m_setting.setCreateScriptBefore(v==Qt::Checked);
    }
}
void SettingDlg::on_defaultSaveFormat_currentTextChanged(const QString& v){
    if(m_setting.getDefaultSaveFormat() != v){
        m_setting.setDefaultSaveFormat(v);
    }
}
void SettingDlg::on_defaultSavePatern_textChanged(const QString& v){
    if(m_setting.getDefaultSavePatern() != v){
        m_setting.setDefaultSavePatern(v);
    }
}
void SettingDlg::on_margingBottom_valueChanged(const double& v){
    if(m_setting.getMargingBottom() != v){
        m_setting.setMargingBottom(v);
    }
}
void SettingDlg::on_margingLeft_valueChanged(const double& v){
    if(m_setting.getMargingLeft() != v){
        m_setting.setMargingLeft(v);
    }
}
void SettingDlg::on_margingRight_valueChanged(const double& v){
    if(m_setting.getMargingRight() != v){
        m_setting.setMargingRight(v);
    }
}
void SettingDlg::on_margingTop_valueChanged(const double& v){
    if(m_setting.getMargingTop() != v){
        m_setting.setMargingTop(v);
    }
}
void SettingDlg::on_mdiShowMode_currentIndexChanged(const int& v){
    if(m_setting.getMdiShowMode() != v){
        m_setting.setMdiShowMode(v);
    }
}
void SettingDlg::on_orientation_currentIndexChanged(const int& v){
    if(m_setting.getOrientation() != v){
        m_setting.setOrientation(v);
    }
}
void SettingDlg::on_paperSize_currentIndexChanged(const int& v){
    if(m_setting.getPaperSize() != v){
        m_setting.setPaperSize(v);
    }
}
void SettingDlg::on_resolution_currentIndexChanged(const int& idx){
    int v = widget.resolution->itemData(idx).toInt();
    if(m_setting.getResolution() != v){
        m_setting.setResolution(v);
    }
}
void SettingDlg::on_scriptBeforePatern_textChanged(const QString& v){
    if(m_setting.getScriptBeforePatern() != v){
        m_setting.setScriptBeforePatern(v);
    }
}
